﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;

namespace UniRlv.Utility.AppLib
{
    public class PLCDevice:AbtDODevice
    {
        SerialPort serial = new SerialPort();
        private int port;
        private int bd;

        public override string InitString
        {
            get
            {
                return _initString;
            }
            set
            {
                string[] paramStrings = value.Split(',');

                if (paramStrings.Length > 1)
                {
                    port = Convert.ToInt32(paramStrings[0]);
                    bd = Convert.ToInt32(paramStrings[1]);

                    _initString = value;
                }
            }
        }

        /// <summary>
        /// 打开串口
        /// </summary>
        /// <returns></returns>
        public override bool Open()
        {
            if (IsOpen())
            {
                return true;
            }
            else
            {
                serial.PortName = string.Format("COM{0}", port);
                serial.BaudRate = bd;
                serial.RtsEnable = true;

                try
                {
                    serial.Open();

                    return true;
                }
                catch (System.Exception ex)
                {
                    ErrorString = ex.Message;
                    return false;
                }
            }
        }

        public override bool IsOpen()
        {
            return serial.IsOpen;
        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public override void Close()
        {
            if (serial.IsOpen)
            {
                serial.Close();
            }
        }

        public override void Pause()
        {
        }

        public override void Resume()
        {
        }

        public override void SendData(byte[] bytes, int len)
        {
            serial.Write(bytes, 0, len);
        }

        public override void Alarm(int nStation, object obj)
        {

            if (nStation == 1)
            {
                SendData("=CMD;BTLALARM;END\n");
            }
            else if(nStation == 2)
            {
                SendData("=CMD;BOXALARM;END\n");
            }
        }

        public override void Pick(int nStation, object obj)
        {
            int lp = Convert.ToInt32(obj);

            if (nStation == 1)
            {
                if (lp<=0)
                {
                    SendData("=CMD;BTLIME;END\n");
                }
                else
                {
                    SendData("=CMD;BTLPICK;END\n");
                }
                
            }
            else if (nStation == 2)
            {
                if (lp <= 0)
                {
                    SendData("=CMD;BOXIME;END\n");
                }
                else
                {
                    SendData("=CMD;BOXPICK;END\n");
                }
            }
        }

        public override void Flip(int nStation, object obj)
        {
            SendData("=CMD;BTLFLIP;END\n");
        }

        public override void Stop(int nStation, object obj)
        {
            SendData("=CMD;Stop;END\n");
        }

        public override void OutPut(int pin)
        {
            SendData("=CMD;Out" + pin + ";END\n");
        }
    }
}
